﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;

namespace Receive
{
    public class Program
    {
        public static void Main(string[] args)
        {
            const string queueName = "hello";
            Console.WriteLine("receive start");
            var factory = new ConnectionFactory();
            factory.HostName = "192.168.64.130";
            factory.Port = 5672;
            factory.UserName = "root";
            factory.Password = "root";
            using (var conn=factory.CreateConnection())
            {
                using (var channel=conn.CreateModel())
                {
                    channel.QueueDeclare(queueName,false,false,false,null);

                    var consumer = new EventingBasicConsumer(channel);

                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;

                        var message = Encoding.UTF8.GetString(body);

                        Console.WriteLine(" [x] Received {0} ", message);
                    };

                    channel.BasicConsume(queueName, 
                        noAck: true,
                        consumer: consumer);

                    Console.WriteLine("receive exit. ");

                    Console.ReadLine();
                }
            }
        }
    }
}
